package com.oocl.ptp;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

/**
 * Hello world!
 * 
 */
public class Producer {

	private static Connection con;
	private static Session session;
	private Destination queue;
	private MessageProducer producer;

	static {
		ConnectionFactory factory = new ActiveMQConnectionFactory(
				"tcp://zha-ita105-w7:61616");
		try {
			con = factory.createConnection();
			con.start();
			System.out.println("connect");
		} catch (JMSException e) {
			e.printStackTrace();
		}// 多线程公用
	}

	public Producer(String queueName) throws JMSException {
		queue = new ActiveMQQueue(queueName);
		session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
		producer = session.createProducer(queue);
		System.out.println("construtor");
	}

	public void sendMsg(String data) throws JMSException {
		TextMessage msg = session.createTextMessage(data);
		producer.send(msg);
	}

}
